/*
 * @Author: 飞越疯人院 353362604@qq.com
 * @Date: 2022-09-02 14:40:24
 * @LastEditors: 飞越疯人院 353362604@qq.com
 * @LastEditTime: 2022-09-22 16:52:12
 * @FilePath: \ReconfigurationAdministration\src\Common\Printer\index.jsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

// !!!打印机
import { message } from 'antd'

// 类别 内容 画布大小
/**
 *  type:               类别        1:图片类别
 *  size:               画布大小
 *  content:            内容
 *  printDensity:       打印浓度    B3S/B21/B1/B203： 1~5 默认3 Z401/B32:1~15 默认8 B50系列:6~15 默认6
 *  printLabelType:     纸张类别    1—缝标纸, 2—⿊标纸, 3—连续纸，4代表定孔纸，5代表透 明纸
 *  printMode:          打印模式    1--热敏，2--热转印
 *  count:              打印数量
 */

const {
    getInstance,
    initSdk,
    getAllPrinters,
    selectPrinter,
    InitDrawingBoard,
    DrawLableImage,
    startJob,
    commitJob,
    endJob,
} = window

let data = {}

// 调用
export const initPrinter = (type, size, content, printDensity, printLabelType, printMode, count, MarkEvent) => {
    data = { type, size, content, printDensity, printLabelType, printMode, count, MarkEvent }
    getInstance()
    judgeState()
}

// 判断是否引入
const judgeState = () => {
    setTimeout(() => {
        if (window.g_websocket?.readyState === 1) initSdkFUNC()
        else judgeState()
    }, 500)
}
// 初始化sdk
const initSdkFUNC = () => {
    initSdk({ fontDir: '' }, ({ resultAck }) => {
        if (resultAck.errorCode !== 0) {
            data.MarkEvent.emit()
            return message.error('初始化打印机失败,请检测打印设备!')
        }
        getAllPrintersFUNC()
    })
}
// 获取打印设备
const getAllPrintersFUNC = () => {
    getAllPrinters(({ resultAck }) => {
        if (resultAck.errorCode !== 0) {
            data.MarkEvent.emit()
            return message.error('未获取到打印设备,请检测设备是否连接!')
        }
        const equipmentList = JSON.parse(resultAck.info)
        selectPrinterFUNC(Object.keys(equipmentList)[0], parseInt(Object.values(equipmentList)[0]))
    })
}
// 选择打印设备
const selectPrinterFUNC = (printerName, port) => {
    selectPrinter(printerName, port, ({ resultAck }) => {
        if (resultAck.errorCode !== 0) {
            data.MarkEvent.emit()
            return message.error('未获取到打印设备,请检测设备是否连接!')
        }
        InitDrawingBoardFUNC()
    })
}
// 初始化画布
const InitDrawingBoardFUNC = () => {
    const { type, size, MarkEvent } = data
    InitDrawingBoard(size, ({ resultAck }) => {
        if (resultAck.errorCode !== 0) {
            MarkEvent.emit()
            return message.error('初始化画布失败,请重试!')
        }
        // TODO 判断打印类别
        if (type === 1) {
            DrawLableImageFUNC()
        }
    })
}

// 绘制图片 DrawLableImage
const DrawLableImageFUNC = () => {
    const { content, MarkEvent } = data
    DrawLableImage(content, ({ resultAck }) => {
        if (resultAck.errorCode !== 0) {
            MarkEvent.emit()
            return message.error('图片绘制失败,请重试!')
        }
        startJobFUNC()
    })
}

// ! 开始打印任务
const startJobFUNC = () => {
    const { printDensity, printLabelType, printMode, count, MarkEvent } = data
    startJob(printDensity, printLabelType, printMode, count, ({ resultAck }) => {
        if (resultAck.errorCode !== 0) {
            MarkEvent.emit()
            return message.error('打印任务启动失败,请重试!')
        }
        commitJobFUNC()
    })
}

// ! 执行打印
const commitJobFUNC = () => {
    const { count, MarkEvent } = data
    let jsonObj = JSON.stringify({ printerImageProcessingInfo: { printQuantity: count } })

    commitJob(null, jsonObj, ({ resultAck }) => {
        if (resultAck.errorCode !== 0) {
            MarkEvent.emit()
            return message.error('打印失败! ')
        } else if (resultAck?.printQuantity) {
            message.success('打印完成!')
            endJob()
            MarkEvent.emit()
        }
    })
}
